//index.js
//获取应用实例
const app = getApp();
const array = require("../../utils/data.js");
const util = require("../../utils/util.js");
Page({
  data: {
    array1: array.data1,
    array2: array.data2,
    nowDate: util.formatTime(new Date).replace(new RegExp("/", "g"), '-'),
    index1: 13,
    index2: 6,
    test:1,
    date: util.formatTime(new Date).replace(new RegExp("/", "g"), '-'),
    motto: 'Hello World',
    calender: "",
    calenderData: '',
    isFirst: app.globalData.isFirst,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  btnClick: function() {
    const _that = this;
    wx.getStorage({
      key: 'sessionId',
      success: function (rest) {
        showData2(rest.data.sessionId)
      },
      fail: function () {
        app.globalData.cb = function (sessionId) {   //取sessionId回调函数
          showData2(sessionId)
        }
      }
    });

    function showData2(sessionId){
      wx.request({
        url: 'https://wx.woqutech.cn/Mobile/Menstruation/setConfig',
        method: "POST", 
        data: { cycle: _that.data.array1[_that.data.index1], days: _that.data.array2[_that.data.index2], sessionId: sessionId},
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        success: function (res) {
          console.log(_that.data.array1[_that.data.index1], _that.data.array2[_that.data.index2], sessionId)
          console.log(res)
        }
      })
        
      wx.setStorage({
        key: 'data',
        data: { cycles: _that.data.array1[_that.data.index1], days: _that.data.array2[_that.data.index2], date: _that.data.date },
        success: function (res) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          });
          wx.getStorage({
            key: 'data',
            success: function (res) {
              //加载到缓存
              wx.showToast({
                title: '加载中',
                icon: 'success',
                duration: 2000
              });
              app.globalData.isFirst = false;
              _that.setData({
                calender: "https://wx.woqutech.cn/calendar/index.html?cycles=" + res.data.cycles + '&days=' + res.data.days + '&date=' + res.data.date + '&ifInit=0&sessionId=' + sessionId,
                isFirst: app.globalData.isFirst,
              });
              wx.setStorage({
                key: 'Url',
                data: { calender: _that.data.calender },
                success: function (res) { }
              });
            },
            fail: function () {
              app.globalData.isFirst = true
            }
          })
        }
      });

      wx.setStorage({
        key: 'setting',
        data: { index1: _that.data.index1, index2: _that.data.index2, date: _that.data.date },
        success: function (res) { }
      })

    };

  },
  onLoad: function () {
    const _that = this;
    wx.getStorage({
      key: 'sessionId',
      success: function (rest) {
        app.globalData.cb = function (sessionId) {   //取sessionId回调函数
          showData(sessionId);
        }
      },
      fail: function () {
        app.globalData.cb = function (sessionId) {   //取sessionId回调函数
          showData(sessionId)
        }
      }
    });

    if (this.data.calender == ''){
      wx.getStorage({
        key: 'Url',
        success: function (res) {
          _that.setData({
            calender:res.data.calender.replace(/ifInit=0/, "ifInit=2")
          })
        }
      })
    }

    function showData(sessionId){  

      if (!app.globalData.isFirst){
        _that.setData({
          calender: "https://wx.woqutech.cn/calendar/index.html?cycles=2&days=15&date=2018-01-01&ifInit=2&sessionId=" + sessionId,
          isFirst: app.globalData.isFirst,
        });
      }

      // wx.getStorage({
      //   key: 'data',
      //   success: function (res) {
      //     //加载到缓存
      //     wx.showToast({
      //       title: '加载中',
      //       icon: 'success',
      //       duration: 2000
      //     });
      //     _that.setData({
      //       calender: "https://wx.woqutech.cn/calendar/index.html?cycles=" + res.data.cycles + '&days=' + res.data.days + '&date=' + res.data.date + '&ifInit=2&sessionId=' + sessionId,
      //       isFirst: app.globalData.isFirst,
      //     });
      //     wx.setStorage({
      //       key: 'Url',
      //       data: { calender: _that.data.calender },
      //       success: function (res) { }
      //     });
      //   },
      //   fail: function () {
      //     console.log('没有更多data')
      //     _that.setData({
      //       isFirst: app.globalData.isFirst
      //     })
      //   }
      // })
    };

  },
  bindPickerChange1: function (e) {
    this.setData({
      index1: e.detail.value,
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      index2: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  onShow:function(){
    var that =this;
    
    wx.getStorage({
      key: 'setting',
      success: function (res) {
        that.setData({
          index1: res.data.index1,
          index2: res.data.index2,
          date: res.data.date,
        })
      },
    });
    this.setData({
      isFirst: app.globalData.isFirst
    });
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  }
})

